package com.bilibili.server.service;

import com.bilibili.autoconfig.template.HuanXinTemplate;
import com.bilibili.commons.Constants;
import com.bilibili.commons.exception.TanHuaException;
import com.bilibili.dubbo.api.db.UserApi;
import com.bilibili.dubbo.api.db.UserInfoApi;
import com.bilibili.dubbo.api.mongo.FriendApi;
import com.bilibili.model.db.User;
import com.bilibili.model.db.UserInfo;
import com.bilibili.model.mongo.Friend;
import com.bilibili.model.vo.ContactVo;
import com.bilibili.model.vo.PageResult;
import com.bilibili.model.vo.UserInfoVo;
import com.bilibili.server.interceptor.UserHolder;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author HuangRongFu
 * 2021/11/23 星期二1:18 下午
 */

@Service
public class MessagesService {

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private FriendApi friendApi;

    /**
     * 根据环信id查询用户信息
     *
     * @param: huanXinId
     * @Return:
     */
    public UserInfoVo findUserInfo(String huanXinId) {

        //通过环信id 查询用户信息
        User user = userApi.findByHuanXin(huanXinId);
        Long userId = user.getId();

        //通过用户id 查询用户详细信息
        UserInfo pojo = userInfoApi.findById(userId);

        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(pojo, vo);

        if (pojo.getAge() != null) {

            vo.setAge(pojo.getAge().toString());
        }

        return vo;
    }

    /**
     * 添加联系人
     *
     * @param: userId
     * @Return:
     */
    public void addContacts(Long userId) {

        //获取登录用户环信id
        User loginUser = userApi.findById(UserHolder.getUserId());

        String loginUserHxUser = loginUser.getHxUser();


        //构建要添加用户的环信id
        String hxId = Constants.HX_USER_PREFIX + userId;

        Boolean aBoolean = huanXinTemplate.addContact(loginUserHxUser, hxId);

        if (!aBoolean) {

            throw new TanHuaException("添加好友失败,请再次尝试添加");
        }

        //成功将好友关系添加到mongoDB
        friendApi.addContact(UserHolder.getUserId(), userId);
    }

    /**
     * 联系人列表
     *
     * @param: page
     * @param: pageSize
     * @param: heyWord
     * @Return: PageResult
     */
    public PageResult<ContactVo> findContact(Long page, Long pageSize, String keyword) {

        //查询用户的好友
        PageResult pageResult = friendApi.findContact(page, pageSize, UserHolder.getUserId());

        //好友的详细信息
        List<Friend> friendList = pageResult.getItems();

        //如果为空就返回一个空值
        if (CollectionUtils.isEmpty(friendList)) {

            return new PageResult();
        }

        UserInfo userInfoCondition = new UserInfo();
        userInfoCondition.setNickname(keyword);

        //不为空 取出好友id
        List<Long> friendIds = friendList.stream().map(Friend::getFriendId).collect(Collectors.toList());

        Map<Long, UserInfo> friendMap = userInfoApi.findByIds(friendIds, userInfoCondition);

        List<ContactVo> voList = new ArrayList<>();

        for (Long friendId : friendIds) {

            UserInfo userInfo = friendMap.get(friendId);

            voList.add(ContactVo.init(userInfo));
        }

        pageResult.setItems(voList);

        return pageResult;
    }
}
